/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.dao.hibernate;

import br.com.fatecpg.songs.entity.Artist;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import static org.hibernate.criterion.MatchMode.*;
import org.hibernate.criterion.Order;

/**
 *
 * @author Marcos
 */
public class ArtistDAO extends SearchableDAO<Artist> implements br.com.fatecpg.songs.dao.ArtistDAO {

    public ArtistDAO() {
        super(Artist.class);
    }
    /* Consulta de artista por nome, título do album, gravadora, ou nome de música */
    @Override
    public Criterion getSearchCriterion(String searchedWord) {
        Criterion criterion = Restrictions.or(
                Restrictions.ilike("name", searchedWord, ANYWHERE),
                Restrictions.or(
                    Restrictions.ilike("a.title", searchedWord, ANYWHERE),
                    Restrictions.or(                        
                        Restrictions.ilike("g.name", searchedWord, ANYWHERE),
                        Restrictions.ilike("m.name", ANYWHERE)
                    )
                )
        );
        return criterion;
    }

    @Override
    public void setAlias(Criteria c) {
        c.createAlias("musicians", "m", Criteria.LEFT_JOIN)
         .createAlias("albums", "a", Criteria.LEFT_JOIN)
         .createAlias("genre", "g", Criteria.LEFT_JOIN);
    }

    @Override
    public void setOrder(Criteria c) {
        c.addOrder(Order.asc("name"))
                .addOrder(Order.asc("id"));
    }
}
